<template>
  <custom-dialog
    ref="dialog"
    :title="modalTitle" height="400px"
    :before-confirm="beforeConfirm" :on-confirm="onConfirm"
  >
    <el-form ref="form" :model="info" :rules="rules" label-width="120px">
      <el-form-item label="所属应用" prop="applicationId">
        <el-select v-model="info.applicationId" class="width-100" disabled>
          <el-option v-for="option in appsOption" :key="option.id" v-bind="option"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="applicationId" label="上级菜单">
        <el-cascader
          class="width-100" v-model="info.superiorMenuId"
          :options="cascaderOptions"
          :props="{
            label: 'menuTitle',
            value: 'id',
            children: 'children',
            checkStrictly: true
          }"
          @change="onCascaderChange"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="菜单类型" prop="menuType">
        <el-radio-group v-model="info.menuType">
          <el-radio v-for="option in menuTypeOption" :key="option.value" :label="option.value">
            {{ option.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="菜单名称" prop="menuTitle">
        <el-input v-model="info.menuTitle"></el-input>
      </el-form-item>
      <el-form-item label="菜单图标">
        <select-icon :value="info.menuIcon" @selected="info.menuIcon= $event">
          <template #default="{ selectedIcon }">
            <span v-if="selectedIcon">
              <svg-icon :name="selectedIcon" class="selected-icon"></svg-icon>
              {{ selectedIcon }}
              <svg-icon name="close" @click.stop="info.menuIcon = ''"></svg-icon>
            </span>
            <el-button type="primary" v-else>选择图标</el-button>
          </template>
        </select-icon>
      </el-form-item>
      <el-form-item label="菜单路径" prop="menuAddress">
        <el-input v-model="info.menuAddress"></el-input>
      </el-form-item>
      <el-form-item label="菜单标识">
        <el-input v-model="info.menuName"></el-input>
      </el-form-item>
      <el-form-item label="序号">
        <el-input-number v-model="info.xh" :min="0" :step="1"></el-input-number>
      </el-form-item>
    </el-form>
  </custom-dialog>
</template>

<script>
import CustomDialog from '@/components/custom-dialog'
import SelectIcon from '@/components/select-icon'
import { saveOrUpdateMenu, queryMenuTreeList } from '@/api/sysMenu'
import { queryList } from '@/api/sysApplication'
import settings from '@/settings'

const api = { saveOrUpdateMenu, queryList, queryMenuTreeList }
export default {
  name: 'operate-modal',
  props: {
    applicationId: [String, Number],
    modalTitle: String,
    data: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  components: { CustomDialog, SelectIcon },
  data () {
    return {
      info: {
        id: '',
        menuName: '',
        menuTitle: '',
        menuType: 0,
        menuTypeName: '目录',
        superiorMenuId: '',
        menuAddress: '',
        menuIcon: '',
        menuEnable: 0,
        applicationId: '',
        xh: 0
      },
      rules: {
        applicationId: [{ required: true, message: '请选择所属应用' }],
        menuTitle: [{ required: true, message: '请输入菜单名称' }],
        menuType: [{ required: true, message: '请选择菜单类型' }],
        menuAddress: [{ required: true, message: '请输入菜单路径' }]
      },
      menuTypeOption: settings.MENU_TYPE_OPTIONS,
      appsOption: [],
      cascaderOptions: []
    }
  },
  watch: {
    applicationId (val) {
      if (val) {
        this.getCascaderOptions(val)
      }
    }
  },
  mounted () {
    this.getApps()
  },
  methods: {
    getCascaderOptions (applicationId) {
      return api.queryMenuTreeList(applicationId).then(res => {
        if (res.code === 200) {
          this.cascaderOptions = res.data
        }
      })
    },
    getApps () {
      return api.queryList().then(res => {
        if (res.code === 200) {
          this.appsOption = res.data.map(v => ({
            label: v.applicationName,
            value: v.id
          }))
        }
      })
    },
    open () {
      const isAdd = Object.keys(this.data).length === 0
      this.info = isAdd
        ? this.$options.data().info
        : {
            ...this.$options.data().info,
            ...this.data
          }
      if (this.$refs.form) {
        this.$refs.form.resetFields()
      }
      if (this.applicationId) {
        this.info.applicationId = this.applicationId
      }
      this.$nextTick(() => {
        this.$refs.dialog.open()
      })
    },
    beforeConfirm () {
      return this.$refs.form.validate()
    },
    onConfirm (close) {
      const params = { ...this.info }
      if (params.id === '') {
        delete params.id
      }
      const superiorMenuId = params.superiorMenuId
      if (Array.isArray(superiorMenuId)) {
        params.superiorMenuId = superiorMenuId[superiorMenuId.length - 1]
      }
      return api.saveOrUpdateMenu(params).then((res) => {
        if (res.code === 200) {
          // update cascader options
          if (this.applicationId) {
            this.getCascaderOptions(this.applicationId)
          }
          this.$emit('save-success')
          this.$message.success('保存成功')
          close()
        }
      })
    },
    onCascaderChange () {}
  }
}
</script>

<style scoped>

</style>
